/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Interfaz.Altas;

import Controladores.ControladorCamposTexto;
import Controladores.GestorAdministradores;
import Modelo.Administrador;
import java.awt.Color;
import java.awt.event.KeyEvent;
import javax.swing.JOptionPane;

/**
 *
 * @author Marko
 */
public class InterfazAltaAdministrador extends javax.swing.JDialog {

    /**
     * Creates new form InterfazAltaPersonal
     * @param _pantallaPadre
     * @param _modal
     */
    public InterfazAltaAdministrador(
     java.awt.Frame _pantallaPadre,
     boolean _modal
    ) {
        super(_pantallaPadre, "Alta Personal SAPEC", _modal);
        initComponents();

        establecerFondo();
        this.setLocationRelativeTo(_pantallaPadre);     
        administrador = new GestorAdministradores();
        controladorCampos = new ControladorCamposTexto();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jLabelContrasena = new javax.swing.JLabel();
        jLabelNombreUsuario = new javax.swing.JLabel();
        jPasswordFieldComprobarContrasena = new javax.swing.JPasswordField();
        jTextFieldNombreUsuario = new javax.swing.JTextField();
        jLabelConfirmarContrasena = new javax.swing.JLabel();
        jPasswordFieldContrasena = new javax.swing.JPasswordField();
        jLabelNombre = new javax.swing.JLabel();
        jTextFieldNombre = new javax.swing.JTextField();
        jPanel3 = new javax.swing.JPanel();
        jButtonAceptar = new javax.swing.JButton();
        jButtonCancelar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel2.setOpaque(false);

        jLabelContrasena.setText("Contraseña");

        jLabelNombreUsuario.setText("Nombre de usuario");

        jPasswordFieldComprobarContrasena.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jPasswordFieldComprobarContrasenaFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                jPasswordFieldComprobarContrasenaFocusLost(evt);
            }
        });
        jPasswordFieldComprobarContrasena.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jPasswordFieldComprobarContrasenaKeyPressed(evt);
            }
        });

        jTextFieldNombreUsuario.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jTextFieldNombreUsuarioFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                jTextFieldNombreUsuarioFocusLost(evt);
            }
        });
        jTextFieldNombreUsuario.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextFieldNombreUsuarioKeyPressed(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jTextFieldNombreUsuarioKeyTyped(evt);
            }
        });

        jLabelConfirmarContrasena.setText("Confirmar contraseña");

        jPasswordFieldContrasena.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jPasswordFieldContrasenaFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                jPasswordFieldContrasenaFocusLost(evt);
            }
        });
        jPasswordFieldContrasena.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jPasswordFieldContrasenaKeyPressed(evt);
            }
        });

        jLabelNombre.setText("Nombre completo");

        jTextFieldNombre.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jTextFieldNombreFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                jTextFieldNombreFocusLost(evt);
            }
        });
        jTextFieldNombre.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextFieldNombreKeyPressed(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jTextFieldNombreKeyTyped(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabelConfirmarContrasena)
                    .addComponent(jLabelContrasena)
                    .addComponent(jLabelNombreUsuario)
                    .addComponent(jLabelNombre))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jTextFieldNombre, javax.swing.GroupLayout.DEFAULT_SIZE, 120, Short.MAX_VALUE)
                    .addComponent(jTextFieldNombreUsuario)
                    .addComponent(jPasswordFieldContrasena)
                    .addComponent(jPasswordFieldComprobarContrasena))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelNombre)
                    .addComponent(jTextFieldNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTextFieldNombreUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabelNombreUsuario))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabelContrasena)
                    .addComponent(jPasswordFieldContrasena, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelConfirmarContrasena)
                    .addComponent(jPasswordFieldComprobarContrasena, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel3.setOpaque(false);

        jButtonAceptar.setText("Aceptar");
        jButtonAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAceptarActionPerformed(evt);
            }
        });
        jButtonAceptar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jButtonAceptarKeyPressed(evt);
            }
        });

        jButtonCancelar.setText("Cancelar");
        jButtonCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCancelarActionPerformed(evt);
            }
        });
        jButtonCancelar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jButtonCancelarKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButtonAceptar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 94, Short.MAX_VALUE)
                .addComponent(jButtonCancelar)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonAceptar)
                    .addComponent(jButtonCancelar))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAceptarActionPerformed
        administrarNuevoRegistro();
    }//GEN-LAST:event_jButtonAceptarActionPerformed

    private void jButtonAceptarKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jButtonAceptarKeyPressed
        if(evt.getKeyCode() == KeyEvent.VK_ENTER){
            administrarNuevoRegistro();
        }
    }//GEN-LAST:event_jButtonAceptarKeyPressed

    private void jButtonCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCancelarActionPerformed
        cerrar();
    }//GEN-LAST:event_jButtonCancelarActionPerformed

    private void jButtonCancelarKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jButtonCancelarKeyPressed
        if(evt.getKeyCode() == KeyEvent.VK_ENTER){
            cerrar();
        }
    }//GEN-LAST:event_jButtonCancelarKeyPressed

    private void jTextFieldNombreKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextFieldNombreKeyPressed
        controladorCampos.enterTab(evt);
    }//GEN-LAST:event_jTextFieldNombreKeyPressed

    private void jTextFieldNombreKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextFieldNombreKeyTyped
        if(controladorCampos.formatoAlfabetico(evt)){
            controladorCampos.formatoMayusculas(evt);
        }
    }//GEN-LAST:event_jTextFieldNombreKeyTyped

    private void jTextFieldNombreFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextFieldNombreFocusLost
        controladorCampos.esCampoTextoEnBlanco(jTextFieldNombre);
    }//GEN-LAST:event_jTextFieldNombreFocusLost

    private void jTextFieldNombreFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextFieldNombreFocusGained
        controladorCampos.campoEnUso(jTextFieldNombre);
    }//GEN-LAST:event_jTextFieldNombreFocusGained

    private void jTextFieldNombreUsuarioKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextFieldNombreUsuarioKeyPressed
        controladorCampos.enterTab(evt);
    }//GEN-LAST:event_jTextFieldNombreUsuarioKeyPressed

    private void jTextFieldNombreUsuarioKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextFieldNombreUsuarioKeyTyped
        if(controladorCampos.formatoAlfaNumerico(evt)){
            controladorCampos.formatoMayusculas(evt);
        }
    }//GEN-LAST:event_jTextFieldNombreUsuarioKeyTyped

    private void jTextFieldNombreUsuarioFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextFieldNombreUsuarioFocusLost
        administrador.comprobarNombreUsuario(jTextFieldNombreUsuario);
    }//GEN-LAST:event_jTextFieldNombreUsuarioFocusLost

    private void jTextFieldNombreUsuarioFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextFieldNombreUsuarioFocusGained
        controladorCampos.campoEnUso(jTextFieldNombreUsuario);
    }//GEN-LAST:event_jTextFieldNombreUsuarioFocusGained

    private void jPasswordFieldContrasenaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jPasswordFieldContrasenaKeyPressed
        controladorCampos.enterTab(evt);
    }//GEN-LAST:event_jPasswordFieldContrasenaKeyPressed

    private void jPasswordFieldContrasenaFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jPasswordFieldContrasenaFocusLost
        administrador.comprobarContrasena(jPasswordFieldContrasena);
    }//GEN-LAST:event_jPasswordFieldContrasenaFocusLost

    private void jPasswordFieldContrasenaFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jPasswordFieldContrasenaFocusGained
        controladorCampos.campoEnUso(jPasswordFieldContrasena);
    }//GEN-LAST:event_jPasswordFieldContrasenaFocusGained

    private void jPasswordFieldComprobarContrasenaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jPasswordFieldComprobarContrasenaKeyPressed
        controladorCampos.enterTab(evt);
    }//GEN-LAST:event_jPasswordFieldComprobarContrasenaKeyPressed

    private void jPasswordFieldComprobarContrasenaFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jPasswordFieldComprobarContrasenaFocusLost
        administrador.compararContrasenas(jPasswordFieldContrasena, jPasswordFieldComprobarContrasena);
    }//GEN-LAST:event_jPasswordFieldComprobarContrasenaFocusLost

    private void jPasswordFieldComprobarContrasenaFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jPasswordFieldComprobarContrasenaFocusGained
        controladorCampos.campoEnUso(jPasswordFieldComprobarContrasena);
    }//GEN-LAST:event_jPasswordFieldComprobarContrasenaFocusGained
    
    /**
     * crear el nuevo administrador para el registro
     * @return devuelve al nuevo administrador
     */
    private Administrador nuevoAdministrador(){
        Administrador nuevoAdministrador = new Administrador();
        
        String nombre = jTextFieldNombre.getText();
        nuevoAdministrador.establecerNombre(nombre);
        
        String nombreUsuario = jTextFieldNombreUsuario.getText();
        nuevoAdministrador.establecerNombreUsuario(nombreUsuario);
        
        char[] contrasena = jPasswordFieldContrasena.getPassword();
        nuevoAdministrador.establecerContrasena(contrasena);
        
        return nuevoAdministrador;
    }
    
    /**
     * solicita la confirmacion para dar de alta al nuevo administrador
     * @return verdadero en caso de ser aceptada la nueva alta
     */
    private boolean confirmarAlta(){
        boolean AltaConfirmada = true;
        int ConfirmarAlta = JOptionPane.showConfirmDialog(
                    null,
                    "¿Desea dar de alta al nuevo administrador de sapec?",
                    "Alta de presonal SAPEC",
                    JOptionPane.OK_CANCEL_OPTION,
                    JOptionPane.INFORMATION_MESSAGE
                    );
        
        if(ConfirmarAlta == JOptionPane.OK_OPTION){
            return AltaConfirmada;
        }else{
            return !AltaConfirmada;
        }// IF
    }
    
    /**
     * comprueba que todos los campos esten correctos
     * @return devuelve verdadero si y solo si todos los campos son correctos
     */
    private boolean sonCamposCorrectos(){
        boolean camposCorrectos = true;
        
        if(jTextFieldNombre.getToolTipText() == null
                && jTextFieldNombreUsuario.getToolTipText() == null
                && jPasswordFieldContrasena.getToolTipText() == null
                && jPasswordFieldComprobarContrasena.getToolTipText() == null){
            return camposCorrectos;
        }else{
            return !camposCorrectos;
        }
    }
    
    /**
     * administra el proceso de registro del nuevo administrador
     */
    private void administrarNuevoRegistro(){
        if(sonCamposCorrectos()){
            if(confirmarAlta()){
                registrarAdministrador();
            }
        }
    }
    
    /**
     * cierra la ventana
     */
    private void cerrar(){
        dispose();
    }
    
    /**
     * establece el fondo de color blanco
     */
    private void establecerFondo(){
        //cambia el color dle jframe
        this.getContentPane().setBackground(Color.white);
    }
    
    /**
     * solicita el registro del nuevo administrador
     */
    private void registrarAdministrador(){
        Administrador nuevoAdministrador = nuevoAdministrador();
        administrador.registrar(nuevoAdministrador);
        cerrar();
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonAceptar;
    private javax.swing.JButton jButtonCancelar;
    private javax.swing.JLabel jLabelConfirmarContrasena;
    private javax.swing.JLabel jLabelContrasena;
    private javax.swing.JLabel jLabelNombre;
    private javax.swing.JLabel jLabelNombreUsuario;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPasswordField jPasswordFieldComprobarContrasena;
    private javax.swing.JPasswordField jPasswordFieldContrasena;
    private javax.swing.JTextField jTextFieldNombre;
    private javax.swing.JTextField jTextFieldNombreUsuario;
    // End of variables declaration//GEN-END:variables
    private final GestorAdministradores administrador;
    private final ControladorCamposTexto controladorCampos;
}
